package cn.yunhe.news.dao.impl;

import cn.yunhe.news.BaseDB;
import cn.yunhe.news.FenYe;
import cn.yunhe.news.dao.IBoardDao;
import cn.yunhe.news.entity.Board;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BoardDao extends BaseDB implements IBoardDao {
    static Connection conn = null;
    static PreparedStatement psmt = null;
    static ResultSet rs = null;


    @Override
    public List<Board> allBoard() {
        String sql = "select * from tb_board";
        List<Board> boardList = selectAll(sql, null);
        return boardList;
    }

    @Override
    public boolean addBoard(String bname) {
        String sql = "insert into tb_board(c_bname) values(?)";
        Object[] arr = {bname};
        if (updateSql2(sql, arr) > 0) {
            return true;
        } else {
            return false;
        }

    }

    @Override
    public List<Board> keywordBoard(int pageNum,int pageIndex,String keyword) {
        List<Board> keyList = new ArrayList<Board>();
        String sql = "select * from tb_board where c_bname like ? order by c_bid desc limit ?,?";
        String str = "%" + keyword + "%";
        Object[] arr = {str,pageNum * (pageIndex-1),pageNum};
        keyList = selectAll(sql, arr);

        return keyList;
    }

    /**
     * 获得关键字搜索的总条数
     * @param keyword
     * @return
     */
    public int keyAll(String keyword){
        int count=0;
        try{
            String sql = "select count(1) from tb_board where c_bname like ?";
            conn=getConn();
            psmt=conn.prepareStatement(sql);

            String str = "%" + keyword + "%";
            psmt.setObject(1,str);
            rs=psmt.executeQuery();
            if (rs.next())
                count=rs.getInt(1);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return count;
    }



    @Override
    public boolean delBoard(int[] bid) {
        int i;
        for (i = 0; i < bid.length; i++) {
            String sql = "delete from tb_board where c_bid =" + bid[i];
            updateSql2(sql, null);
        }

        if (i == bid.length)
            return true;
        else
            return false;
    }


    @Override
    public List<Board> boardFList(int pageNum, int pageIndex) {
        String sql = "select * from tb_board order by c_bid limit ?,?";
        Integer[] arr={pageNum * (pageIndex -1),pageNum};
        return selectAll(sql,arr);
    }


    /**
     * 返回总条数
     * @return
     */
    public int allCount(){
        return FenYe.boardCount("select count(1) from tb_board");
    }


    /**
     * 查询通用
     *
     * @param sql
     * @param arr
     * @return
     */
    public static List<Board> selectAll(String sql, Object[] arr) {
        List<Board> boardsList = new ArrayList<>();
        try {
            conn = getConn();
            psmt = conn.prepareStatement(sql);
            setZhan(psmt, arr);
            rs = psmt.executeQuery();
            while (rs.next()) {
                Board board = new Board(rs.getInt("c_bid"), rs.getString("c_bname"));
                boardsList.add(board);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(conn, psmt, rs);
        }
        return boardsList;
    }
}